var result = ""; //保存需要运算的第一个数字
var operator = ""; //保存运算符
var isPressEqualsKey = false; //记录是否按下”=“键
//数字键事件
function digital(control) {
	var txt = document.getElementById('txtScream');
	if (isPressEqualsKey) {
		txt.value = ""; //已进行过计算，则清空数值输入框重新开始
		isPressEqualsKey = false;
	}
	//数值输入已经存在小数点,则不允许再输入小数点
	if (txt.value.indexOf('.') > -1 && control == '.') {
		return false;
	}

	//输入00
	if (control == '10') {
		control = '00'
	}

	txt.value += control; //将控件值赋给数值输入框中
}
//退格键事件
function backspace() {
	var txt = document.getElementById('txtScream');
	txt.value = txt.value.substring(0, txt.value.length - 1);
}
//ce键事件：清空数字输入框
function clearAll() {
	document.getElementById('txtScream').value = "";
	result = "";
	operator = "";
}
// +、-、*、/ 事件
function calculation(control) {

	//如果之前存在值，还有运算符
	var txt = document.getElementById('txtScream'); //如果输入框中有值
	if (result != "" && operator != "" && txt.value != "") {

		var opValue; //保存操作结果
		//计算表达式中存在运算符
		var sourseValue = parseFloat(result);
		var txtValue = txt.value; //第二个运算值
		if (operator == 'x') {
			opValue = sourseValue * parseFloat(txtValue);
		} else if (operator == '÷') {
			opValue = sourseValue / parseFloat(txtValue);
		} else if (operator == '+') {
			opValue = sourseValue + parseFloat(txtValue);
		} else if (operator == '-') {
			opValue = sourseValue - parseFloat(txtValue);
		} else if (operator == '%') {
			opValue = sourseValue % parseFloat(txtValue);
		}

		result = parseFloat(opValue); //保存结果
		operator = control; //保存运算符
		txt.value = "";

	} else {
		//将运算符保存入全局变量中
		operator = control;
		var txt = document.getElementById('txtScream');
		if (txt.value == "") return false; //数值输入框中没有数字，则不能输入运算符
		//将数值输入框中的值保存到计算表达式中
		result = txt.value;
		//清空输入框，以待输入操作值
		txt.value = "";
	}

}
//计算结果
function getResult() {
	var opValue;
	//计算表达式中存在运算符
	var sourseValue = parseFloat(result);
	var txt = document.getElementById('txtScream'); //运算结果
	var txtValue = txt.value; //第二个运算值
	var note = document.getElementById('note'); //错误提示
	if (operator == 'x') {
		opValue = sourseValue * parseFloat(txtValue);
	} else if (operator == '÷') {
		opValue = sourseValue / parseFloat(txtValue);
	} else if (operator == '+') {
		opValue = sourseValue + parseFloat(txtValue);
	} else if (operator == '-') {
		opValue = sourseValue - parseFloat(txtValue);
	} else if (operator == '%') {
		opValue = sourseValue % parseFloat(txtValue);
	}

	//判断结果
	if (isNaN(opValue)) {
		note.innerHTML = "操作有误";
		clearAll(); //清空
	} else if (operator == '÷' && txtValue == '0') {
		note.innerHTML = "被除数不能为0";
		clearAll(); //清空保存
	} else {
		//如果不是整数保存两位小数
		txt.value = parseFloat(opValue.toFixed(2));
		note.innerHTML = "";
	}

	//  operator="";//清楚运算符
	isPressEqualsKey = true;
	result = parseFloat(opValue.toFixed(2)); //保存上一个运算结果
	opValue = "";
}

//其他数学函数
function myMath(str) {

	var opValue; //运算结果
	//计算表达式中存在运算符
	var txt = document.getElementById('txtScream'); //运算结果
	var txtValue = txt.value; //运算值
	var note = document.getElementById('note'); //错误提示

	if (txtValue == '') {
		note.innerHTML = "操作有误";
		clearAll(); //清空
	} else if (txtValue != '') {

		if (str == 'cos') {
			opValue = Math.cos(txtValue * Math.PI / 180);
		} else if (str == 'sin') {
			opValue = Math.sin(txtValue * Math.PI / 180);
		} else if (str == 'tan') {
			opValue = Math.tan(txtValue * Math.PI / 180);
		} else if (str == 'x²') {
			opValue = Math.pow(txtValue, 2);
		} else if (str == '√') {
			opValue = Math.sqrt(txtValue);
		}

	} else {
		note.innerHTML = "操作有误";
		clearAll(); //清空
	}

	txt.value = parseFloat(opValue.toFixed(2));
	note.innerHTML = "";

	isPressEqualsKey = true;
	result = parseFloat(opValue.toFixed(2)); //保存上一个运算结果
	opValue = "";

}